<?php

/**
 * Created by Reliese Model.
 */

namespace App\Models\Adminer;

use Carbon\Carbon;
use App\Models\Eloquent;
use App\Models\Meimei\Interfaces\User;
use Illuminate\Http\Request;


/**
 * Class BkzsAdmin
 *
 * @property int $id
 * @property string $name
 * @property string $avatar
 * @property string $phone
 * @property string $password
 * @property string $tk
 * @property int $type
 * @property int $enable
 * @property int $role_id
 * @property string $remark
 * @property Carbon|null $created_at
 * @property Carbon|null $updated_at
 *
 * @package App\Models
 */
class Admins extends Eloquent implements User
{
	protected $table = 'admins';

	protected $casts = [
		'type' => 'int',
		'enable' => 'int',
		'role_id' => 'int'
	];

	protected $hidden = [
		'password', 'tk'
	];

	protected $fillable = [
		'name',
		'avatar',
		'phone',
		'password',
		'type',
		'enable',
		'role_id',
        'tk',
		'remark'
	];

    public function getId()
    {
        return $this->id;
    }

	public static $fillables = [
        'name' => '管理员名称',
        'avatar' => '管理员头像',
        'phone' => '管理员手机号',
        'password' => '管理员密码',
        'type' => '管理员类型',
        'enable' => '管理员状态',
        'role_id' => '管理员角色ID',
        'remark' => '管理员说明',
    ];

    public static function getByMobile(string $phone): ?self
    {
        return self::where('phone', $phone)->first();
    }

    public static function getByTk(string $tk): ?self
    {
        return self::where('tk', $tk)->first();
    }

    /**
     * --获取当前登录管理员
     * @var self
     */
    public function currentAdminer(): ?self
    {
        if (empty(request()->header('token'))) {
            return null;
        }
        $header_tk = request()->header('token');
        $currentAdminer = self::getByTk($header_tk);
        if (!$currentAdminer) {
            return null;
        }
        return $currentAdminer;
    }

    public static function getInfoById($id)
    {
        $adminer = Admins::find($id);
        if(!$adminer) {
            return '';
        }

        return  $adminer->name. '('. $adminer->phone .')';
    }
}
